package net.zhuike.novel.app.ui.fragment;


import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;

import net.zhuike.novel.app.R;
import net.zhuike.novel.app.bean.CategoryList;
import net.zhuike.novel.app.bean.RankingList;
import net.zhuike.novel.app.bean.TabModel;
import net.zhuike.novel.app.bean.TabModelResult;
import net.zhuike.novel.app.common.OnRvItemClickListener;
import net.zhuike.novel.app.service.BookStoreService;
import net.zhuike.novel.app.ui.activity.SearchActivity;
import net.zhuike.novel.app.ui.activity.SubRankActivity;
import net.zhuike.novel.app.ui.adapter.TopCategoryListAdapter;
import net.zhuike.novel.app.ui.adapter.TopRankAdapter;
import net.zhuike.novel.app.ui.base.BaseFragment;
import net.zhuike.novel.app.ui.base.Constant;
import net.zhuike.novel.app.ui.utils.SharedPreferencesUtil;
import net.zhuike.novel.app.ui.view.SupportGridItemDecoration;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import framework.common.views.InnerListView;
import framework.common.zanetwork.core.ResponseCallback;
import okhttp3.Response;

/**
 * Created by wq on 2017/6/10.
 * 其他分类
 */
public class RankingOtherFragment extends BaseFragment{
    ExpandableListView elvFeMale;
    @Bind(R.id.elvMale)
    ExpandableListView elvMale;
    private Context context;
    private int select = 0;
    private List<TabModel> maleGroups = new ArrayList<>();
    List<TabModel>tabList=new ArrayList<>();
    private TopRankAdapter maleAdapter;
    private List<List<TabModel>> femaleChilds = new ArrayList<>();
    @Override
    public int getLayoutResId() {
        return R.layout.fragment_store_other;
    }

    @Override
    public void initDatas() {

    }

    @Override
    public void configViews() {
        context = getActivity();

       // getGender();
        maleAdapter = new TopRankAdapter(getActivity(), maleGroups, femaleChilds);
        maleAdapter.setItemClickListener(new ClickListener());
        elvMale.setAdapter(maleAdapter);

        //分类
        getCategoryList();
    }



    public void getCategoryList() {
        String topCategoryId= SharedPreferencesUtil.getInstance().getString(Constant.TopCategoryId);
        BookStoreService.getTab(context, topCategoryId, new ResponseCallback<TabModelResult>() {
            @Override
            public void onSuccess(TabModelResult advertisesModels) {
                if(advertisesModels!=null){
                    tabList.clear();
                    for(int i=0;i<advertisesModels.getSubCategories().size();i++){
                        TabModel model=advertisesModels.getSubCategories().get(i);
                        if(model.getId().equals("11")){
                            if(model.getSubCategories()!=null);
                            for (int j = 0; j <model.getSubCategories().size(); j++) {
                                TabModel models=model.getSubCategories().get(j);
                                if(models.getId().equals("103")) {
                                    if(models.getSubCategories()!=null) {
                                        tabList.addAll(models.getSubCategories());
                                    }
                                }
                            }

                        }
                    }
                    showRankList(tabList);
                }

            }

            @Override
            public void onError(Response response, int errorCode, String errorMsg, Exception e) {

            }
        });
    }
    public void showRankList(List<TabModel>list) {
        femaleChilds.clear();
        updateMale(list);
    }
    private void updateMale(List<TabModel>list) {
        List<TabModel> lists = list;
        List<TabModel> collapse = new ArrayList<>();
        for (TabModel bean : lists) {
            if (bean.isCollapse()) { // 折叠
                collapse.add(bean);
            } else {
                maleGroups.add(bean);
                femaleChilds.add(new ArrayList<TabModel>());
            }
        }
        maleAdapter.notifyDataSetChanged();
    }

    class ClickListener implements OnRvItemClickListener<TabModel> {

        @Override
        public void onItemClick(View view, int position, TabModel data) {
            String allId = null;
            if (allId == null) {
                allId = data.getId();
            }
            SubRankActivity.startActivity(mContext, allId, data.getName());
        }
    }

}
